package cn.peach.quartz.job;

import cn.peach.order.domain.OrderPet;
import cn.peach.order.domain.OrderPetDetail;
import cn.peach.order.mapper.OrderPetDetailMapper;
import cn.peach.order.mapper.OrderPetMapper;
import cn.peach.pay.domain.PayBill;
import cn.peach.pay.mapper.PayBillMapper;
import cn.peach.pet.domain.Pet;
import cn.peach.pet.mapper.PetMapper;
import cn.peach.quartz.util.QuartzUtil;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单超时的处理
 */
@Component
public class OrderExpireJob implements Job {

    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private OrderPetMapper orderPetMapper;
    @Autowired
    private OrderPetDetailMapper orderPetDetailMapper;
    @Autowired
    private PetMapper petMapper;
    /**
     * 订单超时的后续处理
     * @param jobExecutionContext
     */
    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext){
        /**
         * 拿到数据
         */
        JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, String> params = (Map<String, String>)map.get("params");
        // 获取订单
        String orderNo = params.get("orderNo");
        // 查询支付单
        PayBill payBill = payBillMapper.getByNo(orderNo);
        String jobName = params.get("jobName");
        // 判断状态是否是待支付
        if (payBill.getState().intValue() != PayBill.SATET_WAIT_PAY) {
            QuartzUtil.removeJob(scheduler, jobName);
            return;
        }
        OrderPet orderPet = orderPetMapper.getByOrderNo(orderNo);
        if (orderPet.getState().intValue() != OrderPet.STATE_WAIT_PAY) {
            QuartzUtil.removeJob(scheduler, jobName);
            return;
        }
        // 修改状态
        payBill.setState(PayBill.SATET_CANCEL);
        Date date = new Date();
        payBill.setUpdateTime(date);
        payBillMapper.update(payBill);
        // 查询业务单 修改状态
        orderPet.setState(OrderPet.STATE_PAY_OUTTIME_CANCEL);
        orderPet.setLastUpdateTime(date);
        orderPetMapper.update(orderPet);

        // 查出宠物 退库存 要拿到支付明细
        List<OrderPetDetail> details = orderPetDetailMapper.getByOrderNo(orderNo);
        // 拿到宠物id的集合
        List<Long> pets = details.stream().map(detail -> detail.getPetId()).collect(Collectors.toList());
        List<Pet> petList = petMapper.patchSelectById(pets, Pet.STATE_ONLINE);
        petList.forEach(pet -> {
            // 退库存
            pet.setStore_number(pet.getStore_number() + 1);
            petMapper.update(pet);
        });
        // 支付宝关单接口
        try {
            AlipayTradeCloseResponse close = Factory.Payment.Common().close(orderNo);
            System.out.println("关单" + close);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 删除定时任务
        QuartzUtil.removeJob(scheduler, jobName);
        System.out.println("干掉了定时任务------");
    }
}
